package table

import (
	"TD-UI/api"
	v1 "TD-UI/api/v1"
	"TD-UI/internal/model"
	"TD-UI/internal/service"
	"context"
	"github.com/gogf/gf/v2/util/gconv"
)

type cTable struct{}

func NewV1() api.ITableV1 {
	return &cTable{}
}

// GetTable implements api.ITableV1.
func (c *cTable) GetTable(ctx context.Context, req *v1.TableReq) (res *v1.TableRes, err error) {
	tData := model.TableReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Table().GetTable(ctx, tData)
	gconv.Struct(result, &res)
	return
}

func (c *cTable) GetTableData(ctx context.Context, req *v1.TableDataReq) (res *v1.TableDataRes, err error) {
	tData := model.TableDataReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Table().GetTableData(ctx, tData)
	gconv.Struct(result, &res)
	return
}

func (c *cTable) GetTableColumns(ctx context.Context, req *v1.TableColumnsReq) (res *v1.TableDataRes, err error) {
	tData := model.TableDataReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Table().GetTableColumns(ctx, tData)
	gconv.Struct(result, &res)
	return
}

func (c *cTable) QueryTableData(ctx context.Context, req *v1.TableDataQueryReq) (res *v1.TableDataRes, err error) {
	tData := model.TableDataQueryReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Table().QueryTableData(ctx, tData)
	gconv.Struct(result, &res)
	return
}

func (c *cTable) DelTableData(ctx context.Context, req *v1.TableDelReq) (res *v1.TableRes, err error) {
	tData := model.TableDelReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Table().DelTable(ctx, tData)
	gconv.Struct(result, &res)
	return
}

func (c *cTable) DdlTableData(ctx context.Context, req *v1.TableDdlReq) (res *v1.TableRes, err error) {
	tData := model.TableDdlReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Table().DdlTable(ctx, tData)
	gconv.Struct(result, &res)
	return
}

func (c *cTable) CreateTableData(ctx context.Context, req *v1.TableCreateReq) (res *v1.TableRes, err error) {
	tData := model.TableCreateReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Table().CreateTable(ctx, tData)
	gconv.Struct(result, &res)
	return
}

func (c *cTable) ExecSqlTableData(ctx context.Context, req *v1.TableExecSqlReq) (res *v1.TableRes, err error) {
	tData := model.TableExecSqlReq{}
	err = gconv.Struct(req, &tData)
	if err != nil {
		panic(err)
	}
	result, err := service.Table().ExecSqlTableData(ctx, tData)
	gconv.Struct(result, &res)
	return
}
